class GroupsController < ApplicationController
  before_filter :require_admin
  def create
    @group = Group.create!(params[:group])
    # brak powiadomienia o stworzeniu - zaznaczenie efektem na liscie.

    # obsluga wyjatku, jesli rekord jest niewlasciwy
    rescue ActiveRecord::RecordInvalid => e
      respond_to do |format|
        format.js {
          render :update do |page|
            page.replace_html :group_errors, e.message[19..-1]
            page.visual_effect :highlight, :group_errors, :duration => 2,
             :startcolor => "#ff0000", :endcolor => "#FFFF00"
          end   }
      end      
    end
  
  def edit
    @group = Group.find(params[:id])
  end

  def update
    @group = Group.find(params[:group][:id])
    respond_to do|format|
      if @group.update_attributes(params[:group])
        format.js
        format.html { 
          flash[:notice] = 'Pomyślnie zaktualizowano grupę.'
          redirect_to :controller => 'admin' }              
      end
    end
    rescue ActiveRecord::RecordInvalid => e
      respond_to do |format|
        format.js {
          render :update do |page|
            page.replace_html :group_errors, e.message[19..-1]
            page.visual_effect :highlight, :group_errors, :duration => 2,
             :startcolor => "#ff0000", :endcolor => "#FFFF00"
          end   }
    end
  end

  def delete
    @group = Group.find(params[:id])
    @group.destroy    
    flash[:notice] = 'Usunięto'
    respond_to do |format|
      format.html { redirect_to :controller => 'admin', :action => 'settings'}
      format.js
    end   
  end
  
end
